package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.PMF;
import vrp.SaleOrder;
import vrp.Customer;
import vrp.Product;

public class SaleOrderEditServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {

    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
    	String responseRedirect = "/saleorders.jsp";
    	
        try {
        	long saleOrderID = Integer.parseInt(req.getParameter("saleOrder"));
        	long partnerID = Integer.parseInt(req.getParameter("client"));
        	long customerID = Integer.parseInt(req.getParameter("customer"));
        	long productID = Integer.parseInt(req.getParameter("product"));
        	int quantity = Integer.parseInt(req.getParameter("quantity"));
        	int recurringType = Integer.parseInt(req.getParameter("recurringType"));
            Date startDate = df.parse(req.getParameter("startDate"));
            Date endDate = df.parse(req.getParameter("endDate"));
            
            if (quantity<=0) {
            	pm.close();
            	resp.sendRedirect("/saleorders.jsp?error=quantity");
            	return;
            }
            if (recurringType<0) {
            	pm.close();
            	resp.sendRedirect("/saleorders.jsp?error=recurringType");
            	return;
            }
            if (endDate.before(startDate)) {
            	pm.close();
            	resp.sendRedirect("/saleorders.jsp?error=recurringDate");
            	return;
            }
        	Key saleOrderKey = KeyFactory.createKey(SaleOrder.class.getSimpleName(), saleOrderID);
            SaleOrder saleOrder = pm.getObjectById(SaleOrder.class, saleOrderKey);
            Key customerKey = KeyFactory.createKey(Customer.class.getSimpleName(), customerID);
            Key productKey = KeyFactory.createKey(Product.class.getSimpleName(), productID);
            
            saleOrder.setCustomer(customerKey);
            saleOrder.setProduct(productKey);
            saleOrder.setQuantity(quantity);
            saleOrder.setRecurringType(recurringType);
            saleOrder.setStartDate(startDate);
            saleOrder.setEndDate(endDate);
            Date dateNow = new Date();
            if (startDate.after(dateNow)){
            	saleOrder.setNextDeliveryDate(startDate);
            }
            saleOrder.setModifiedAt(dateNow);
            pm.makePersistent(saleOrder);
            responseRedirect += "?client=" + partnerID;
        } catch (ParseException e) {
			e.printStackTrace();
		} finally {
            pm.close();
        }
        resp.sendRedirect(responseRedirect);
    }
}